package com.zzl.car.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzl.car.entity.CarBaseInfo;
import com.zzl.car.entity.CarDetailInfo;
import com.zzl.car.entity.CarOrder;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.mapper.CarDetailInfoMapper;
import com.zzl.car.mapper.ZzwCarBaseInfoMapper;
import com.zzl.car.mapper.ZzwCarDetailInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ZzwCarDetailInfoService {

    @Autowired
    private ZzwCarDetailInfoMapper carDetailInfoMapper;
    @Autowired
    private CarDetailInfoMapper cdim;

    // 获取所有详细车辆信息
    public PageInfo<CarDetailInfo> getAllCarDetailInfoByPage(int currentPage,int pageAmount){

        PageInfo<CarDetailInfo> pageInfo = null;
        List<CarDetailInfo> carDetailInfos = new ArrayList<>();

        PageHelper.startPage(currentPage,pageAmount);
        carDetailInfos = carDetailInfoMapper.getAllCarDetailInfo();

        pageInfo = new PageInfo<>(carDetailInfos);
        pageInfo.setList(carDetailInfos);
        return pageInfo;
    }

    // 获取所有详细车辆信息
    public List<CarDetailInfo> getAllCarDetailInfo(){
        return carDetailInfoMapper.getAllCarDetailInfo();
    }

    // 添加车辆具体信息
    public String addCarDetailInfo(Map map){

        // 根据 车牌号 或 发动机号 来判断是否重复
        List<CarDetailInfo> carDetailInfos = getAllCarDetailInfo();

        for(CarDetailInfo c : carDetailInfos) {
            if(c.getCarPlate().equals(map.get("carPlate")) || c.getCarEngine().equals(map.get("carEngine")))
                // 有相同信息，抛出异常
                throw new ZdpException(2911,"添加失败，存在相同 车牌号 或 发动机号 的车辆信息");
        }

        int id = Integer.valueOf(String.valueOf(System.currentTimeMillis()).substring(3,9));

        CarDetailInfo carDetailInfo = new CarDetailInfo();
        carDetailInfo.setId(id);
        carDetailInfo.setCarBaseInfoId(Integer.valueOf(map.get("id").toString()));
        carDetailInfo.setRent(Double.valueOf(map.get("rent").toString()));
        carDetailInfo.setDiscount(Double.valueOf(map.get("discount").toString()));
        carDetailInfo.setCarPlate(map.get("carPlate").toString());
        carDetailInfo.setCarEngine(map.get("carEngine").toString());
        carDetailInfo.setState("0");

        int result = carDetailInfoMapper.addCarDetailInfo(carDetailInfo);
        System.out.println("result -->" + result);
        if(result > 0)
            return "1";
        else
            return "0";
    }

    /**
     * 通过id 修改车辆详细信息
     * @param map
     * @return
     */
    public String modifyCarDetailInfoById(Map<String, Object> map){
        CarDetailInfo carDetailInfo = new CarDetailInfo();
        carDetailInfo.setId(Integer.valueOf(map.get("id").toString()));
        carDetailInfo.setRent(Double.valueOf(map.get("rent").toString()));
        carDetailInfo.setDiscount(Double.valueOf(map.get("discount").toString()));
        carDetailInfo.setCarPlate(map.get("carPlate").toString());
        carDetailInfo.setCarEngine(map.get("carEngine").toString());
        carDetailInfo.setState(map.get("state").toString());

        int result = carDetailInfoMapper.modifyCarDetailInfoById(carDetailInfo);
        System.out.println("result -->" + result);
        if(result > 0)
            return "1";

        throw new ZdpException(2929,"修改失败");
    }

    // 条件模糊查询 carPlate state
    public PageInfo<CarDetailInfo> searchCarDetailInfo(String carPlate,Integer state,int currentPage){

        int pageAmount = 8;
        PageInfo<CarDetailInfo> pageInfo = null;
        List<CarDetailInfo> carBaseInfos = new ArrayList<>();

        PageHelper.startPage(currentPage,pageAmount);
        carBaseInfos = carDetailInfoMapper.searchCarDetailInfo(carPlate,state);

        pageInfo = new PageInfo<>(carBaseInfos);
        pageInfo.setList(carBaseInfos);

        return pageInfo;
    }

    // 删除服务保障信息
    public String deleteCarDetailInfo(Integer id) {

        if(cdim.getCarDetailInfoById(id).getState().equals("1"))
            throw new ZdpException(2949,"车辆租赁中，无法删除");

        if(carDetailInfoMapper.getCarOrderByCarDetailInfoId(id).size() != 0){
            throw new ZdpException(2950,"修改失败，租赁订单中包含该车辆具体信息，无法删除");
        }

        if(carDetailInfoMapper.deleteCarDetailInfo(id) > 0)
            return "1";
        throw new ZdpException(2949,"删除失败");
    }

    /*
     * 通过carDetailInfoId 获取 carOder 中的信息
     */
    public List<CarOrder> getCarOrderByCarDetailInfoId(Integer id){
        return carDetailInfoMapper.getCarOrderByCarDetailInfoId(id);
    }
}
